﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MethodWorx.Core.Serialization;
using MethodWorx.CMS.Content;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.Plugins;

namespace MethodWorx.CMS.UI.Tests.Serialization
{
    [TestClass]
    public class AggregateXmlSerializerTests
    {
        private IEnumerable<Entity> SerializeAndDeserializeEntities(PageTemplate template)
        {
            var serializer = new AggregateXmlSerializer();

            serializer.AddRelationshipResolver<PageTemplate, Property>((p, c) => p.Properties.Add(c.Name, c.Mandatory, c.Type, c.Group, c.Custom));

            var repository = new PageTemplateRepository(new PageTemplate[]
            {
               template
            }.AsQueryable());

            var registry = new RepositoryRegistry();
            registry.RegisterRepository<PageTemplate, PageTemplate>(repository);

            var xml = serializer.Serialize(template);
            Console.Write(xml);

            var output = serializer.Deserialize(new Type[] { typeof(PageTemplate), typeof(Property) }, registry, xml);
            return output;
        }

        [TestMethod]
        public void TestSerializePageTemplate()
        {
            PageTemplate template = new PageTemplate()
            {
                Id = Guid.NewGuid()
            };

            var first = this.SerializeAndDeserializeEntities(template).First();
            Assert.AreEqual(template,first);
           
        }


        [TestMethod]
        public void TestSerializePageTemplateParent()
        {
            PageTemplate template = new PageTemplate()
            {
                Id = Guid.NewGuid(),
                Parent = new PageTemplate()
                {
                    Id = Guid.NewGuid()
                }
            };

            var first = this.SerializeAndDeserializeEntities(template).First();

            Assert.AreEqual(template.Parent,((PageTemplate)first).Parent);
        }

        [TestMethod]
        public void TestSerializePageTemplateParentTwice()
        {
            PageTemplate template = new PageTemplate()
            {
                Id = Guid.NewGuid(),
                Parent = new PageTemplate()
                {
                    Id = Guid.NewGuid()
                }
            };

            template.Parent.Parent = template.Parent;

            var first = this.SerializeAndDeserializeEntities(template).First();

            Assert.AreEqual(template.Parent.Parent, ((PageTemplate)first).Parent.Parent);
        }

        [TestMethod]
        public void TestSerializePageTemplateWithSerializedProperty()
        {
            PageTemplate template = new PageTemplate()
            {
                Id = Guid.NewGuid(),
                Bindings = new PageTemplateBindings()
                {
                    EnableAspDotNetBindings = true,
                    UrlRewritePath = "Bindings"
                }
            };


            var first = this.SerializeAndDeserializeEntities(template).First();

            Assert.AreEqual(template.Bindings.EnableAspDotNetBindings, ((PageTemplate)first).Bindings.EnableAspDotNetBindings);
            Assert.AreEqual(template.Bindings.UrlRewritePath, ((PageTemplate)first).Bindings.UrlRewritePath);
        }

        [TestMethod]
        public void TestSerializePageTemplateWithChildEntityCollection()
        {
            PageTemplate template = new PageTemplate()
            {
                Id = Guid.NewGuid(),
            };

            template.Properties.Add("A Property", true, "Property Type", "Property Group", new PlainTextPropertyDetails());


            var first = this.SerializeAndDeserializeEntities(template).First();

            Assert.AreEqual(template.Properties.First().Name, ((PageTemplate)first).Properties.First().Name);
            Assert.AreEqual(template.Properties.First().Mandatory, ((PageTemplate)first).Properties.First().Mandatory);
            Assert.AreEqual(template.Properties.First().Type, ((PageTemplate)first).Properties.First().Type);
            Assert.AreEqual(template.Properties.First().Group, ((PageTemplate)first).Properties.First().Group);

        }
    }
}
